package com.mitrais.training.yoga;

import javax.swing.JOptionPane;

public class Exercise {

	private static String numberOne(int input) {
		String output = "";
		for (int i = 1; i <= input; i++) {
			int j = 5;
			while (j > 0) {
				output = output + i;
				j--;
			}
		}
		return output;
	}

	private static String numberTwo(int input) {
		String output = "";
		for (int i = 1; i <= input; i++) {
			int j = i;
			while (j > 0) {
				output = output + i;
				j--;
			}
		}
		return output;
	}

	private static String numberThree(int input) {
		String output = "";
		int tempOutput = input;
		for (int i = 1; i <= input; i++) {
			int j = i;

			while (j > 0) {
				output = output + tempOutput;
				j--;
			}
			tempOutput--;
		}
		return output;
	}

	// private enum Fruit {
	// *, +, /, -;
	// }

	private static String calculate(String firstNumber, String operator,
			String secondNumber) {
		String resultStr = "";
		double resultDouble = 0;
		switch (operator.charAt(0)) {
		case '/':
			resultDouble = Double.parseDouble(firstNumber)
					/ Double.parseDouble(secondNumber);
			resultStr = Double.toString(resultDouble);
			break;
		case '*':
			resultDouble = Double.parseDouble(firstNumber)
					* Double.parseDouble(secondNumber);
			resultStr = Double.toString(resultDouble);
			break;
		case '+':
			resultDouble = Double.parseDouble(firstNumber)
					+ Double.parseDouble(secondNumber);
			resultStr = Double.toString(resultDouble);
			break;
		case '-':
			resultDouble = Double.parseDouble(firstNumber)
					- Double.parseDouble(secondNumber);
			resultStr = Double.toString(resultDouble);
			break;
		default:
			JOptionPane.showMessageDialog(null, "Invalid Operator",
					"Error", JOptionPane.ERROR_MESSAGE);
			break;
		}
		return resultStr;
	}

	private static void showDialog() {
		boolean inputAccepted = false;
		while (!inputAccepted) {
			try {
				String firstNumberStr = (String) JOptionPane.showInputDialog(
						"First Number", 0);
				String operatorStr = (String) JOptionPane
						.showInputDialog("Operator e.g. (-),(+),(*),(/)");
				String secondNumberStr = (String) JOptionPane.showInputDialog(
						"Second Number", 0);
				
				if (!firstNumberStr.equals("") && !secondNumberStr.equals("") && !operatorStr.equals("") && !calculate(firstNumberStr, operatorStr,
						secondNumberStr).equals("Invalid operator")) {
					inputAccepted = true;
				} else {
					continue;
				}
				System.out.println(calculate(firstNumberStr, operatorStr,
						secondNumberStr));
			} catch (NumberFormatException nfe) {
				//nfe.printStackTrace();
				JOptionPane.showMessageDialog(null, "Field could not be empty",
						"Error", JOptionPane.ERROR_MESSAGE);
			} catch (NullPointerException npe) {
				System.out.println("Cancelled");
				System.exit(0);
			} catch (StringIndexOutOfBoundsException sioobe){
				JOptionPane.showMessageDialog(null, "Field could not be empty",
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Number 1");
		System.out.println("" + numberOne(6) + "\n");
		System.out.println("Number 2");
		System.out.println("" + numberTwo(6) + "\n");
		System.out.println("Number 3");
		System.out.println("" + numberThree(6) + "\n");
		System.out.println("Number 4");
		showDialog();
	}

}
